package toutiao

import (
	//"encoding/json"
	"github.com/astaxie/beego"
	"github.com/monnand/goredis"
	"log"
)

const (
	TOUTIAO_URL_QUEUE = "toutiao_url_queue"
	TOUTIAO_URL_SET   = "toutiao_url_set"
)

var client goredis.Client

func init() {
	var redisConfig string
	redisConfigHost := beego.AppConfig.String("redis_host")
	redisConfigPort := beego.AppConfig.String("redis_port")
	redisConfig = redisConfigHost + ":" + redisConfigPort

	ConnectRedis(string(redisConfig))
}

func ConnectRedis(redisConfig string) {
	log.Println(">>>连接redis================")
	client.Addr = redisConfig
	client.Db = 16
	log.Println(redisConfig)
}

func GetUrlQueueLength() int {
	length, err := client.Llen(TOUTIAO_URL_QUEUE)
	if err != nil {
		return 0
	}

	return length
}

func PopFromUrlQueue() string {
	res, err := client.Rpop(TOUTIAO_URL_QUEUE)
	if err != nil {
		panic(err)
	}

	return string(res)
}

func PutinUrlQueue(url string) {
	client.Lpush(TOUTIAO_URL_QUEUE, []byte(url))
}

//判断是否在集合（是否已经抓取）
func IsVisit(url string) bool {
	isVisit, err := client.Sismember(TOUTIAO_URL_SET, []byte(url))
	if err != nil {
		return false
	}

	return isVisit
}

//添加集合
func AddToUrlSet(url string) {
	client.Sadd(TOUTIAO_URL_SET, []byte(url))
}
